home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW re2c 1.1 / parser.y < prev    next >
Encoding:
Lex Description  |  1995-06-01  |  2.2 KB  |  149 lines  |  [TEXT/MPS ]

  1. %{
  2.  
  3. // $Log: parser.y,v $
  4. //Revision 1.1  1994/04/08  15:49:34  peter
  5. //Initial revision
  6. //
  7.  
  8. #include <time.h>
  9. #include <iostream.h>
  10. #include "globals.h"
  11. #include "parser.h"
  12. int yyparse();
  13. int yylex();
  14. void yyerror(char*);
  15.  
  16. static uint accept;
  17. static RegExp *spec;
  18. static Scanner *in;
  19.  
  20. %}
  21.  
  22. %start    spec
  23.  
  24. %union {
  25.     Symbol    *symbol;
  26.     RegExp    *regexp;
  27.     Token    *token;
  28.     char    op;
  29. }
  30.  
  31. %token        CLOSE    ID    CODE    RANGE    STRING
  32.  
  33. %type    <op>        CLOSE
  34. %type    <op>        close
  35. %type    <symbol>    ID
  36. %type    <token>        CODE
  37. %type    <regexp>    RANGE    STRING
  38. %type    <regexp>    rule    look    expr    diff    term    factor    primary
  39.  
  40. %%
  41.  
  42. spec    :
  43.         { accept = 0;
  44.           spec = NULL; }
  45.     |    spec rule
  46.         { spec = spec? mkAlt(spec, $2) : $2; }
  47.     |    spec decl
  48.     ;
  49.  
  50. decl    :    ID '=' expr ';'
  51.         { if($1->re)
  52.               in->fatal("sym already defined");
  53.           $1->re = $3; }
  54.     ;
  55.  
  56. rule    :    expr look CODE
  57.         { $$ = new RuleOp($1, $2, $3, accept++); }
  58.     ;
  59.  
  60. look    :
  61.         { $$ = new NullOp; }
  62.     |    '/' expr
  63.         { $$ = $2; }
  64.     ;
  65.  
  66. expr    :    diff
  67.         { $$ = $1; }
  68.     |    expr '|' diff
  69.         { $$ =  mkAlt($1, $3); }
  70.     ;
  71.  
  72. diff    :    term
  73.         { $$ = $1; }
  74.     |    diff '\\' term
  75.         { $$ =  mkDiff($1, $3);
  76.           if(!$$)
  77.                in->fatal("can only difference char sets");
  78.         }
  79.     ;
  80.  
  81. term    :    factor
  82.         { $$ = $1; }
  83.     |    term factor
  84.         { $$ = new CatOp($1, $2); }
  85.     ;
  86.  
  87. factor    :    primary
  88.         { $$ = $1; }
  89.     |    primary close
  90.         {
  91.             switch($2){
  92.             case '*':
  93.             $$ = mkAlt(new CloseOp($1), new NullOp());
  94.             break;
  95.             case '+':
  96.             $$ = new CloseOp($1);
  97.             break;
  98.             case '?':
  99.             $$ = mkAlt($1, new NullOp());
  100.             break;
  101.             }
  102.         }
  103.     ;
  104.  
  105. close    :    CLOSE
  106.         { $$ = $1; }
  107.     |    close CLOSE
  108.         { $$ = ($1 == $2) ? $1 : '*'; }
  109.     ;
  110.  
  111. primary    :    ID
  112.         { if(!$1->re)
  113.               in->fatal("can't find symbol");
  114.           $$ = $1->re; }
  115.     |    RANGE
  116.         { $$ = $1; }
  117.     |    STRING
  118.         { $$ = $1; }
  119.     |    '(' expr ')'
  120.         { $$ = $2; }
  121.     ;
  122.  
  123. %%
  124.  
  125. void yyerror(char* s){
  126.     in->fatal(s);
  127. }
  128.  
  129. int yylex(){
  130.     return in->scan();
  131. }
  132.  
  133. void parse(int i, ostream &o){
  134.     o << "/* Generated by re2c 0.5 on ";
  135.     time_t now = time(&now);
  136.     o.write(ctime(&now), 24);
  137.     o << " */\n";
  138.  
  139.     in = new Scanner(i);
  140.     o << "#line " << in->line() << " \"" << (fileName? fileName : "<stdin>") << "\"\n";
  141.  
  142.     while(in->echo(o)){
  143.     yyparse();
  144.     if(spec)
  145.         genCode(o, spec);
  146.     o << "#line " << in->line() << "\n";
  147.     }
  148. }
  149.